package com.example.web.topic;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.example.web.config.MQConfig;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Consumer extends MQConfig{

	public static void main(String[] args) throws JMSException {
		//创建会话
		Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
		//创建目的地
		Destination destination = session.createTopic(MQConfig.topicName);
		//创建订阅者
		MessageConsumer consumer = session.createConsumer(destination);
		//接收主题
		consumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message message) {
				 TextMessage textMessage = (TextMessage) message;
				 try {
					log.info("接收消息：{}",textMessage.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
		//connection.close();
	}
	
}
